#include "placemangerwindow.h"
#include <QAction>
#include <QToolBar>
#include <QHeaderView>
#include <QStringList>
#include <QDebug>
#include "placeinfowid.h"
#include <QMessageBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QIcon>

PlaceMangerWindow::PlaceMangerWindow(QWidget *parent) : QMainWindow(parent)
{
    this->addWid = new AddPlaceWid();
    this->editWid = new EditPlaceWid();
    this->seaWid = new SearchPlaceWid(this);
    this->addWid->hide();
    this->editWid->hide();
//    this->searchNameLab = new QLabel("请输入查找的地点名称:",this);
//    this->searchNameEdit = new QLineEdit(this);
//    this->confirmBtn = new QPushButton("查找",this);

    this->hide();

    resize(800,600);
    this->setWindowTitle("校园景点管理");
    this->barInit();
    this->tableInit();
    this->seaWid->resize(800,50);
    this->seaWid->move(0,40);
    //布局
//    QHBoxLayout *hlayout = new QHBoxLayout();
//    QVBoxLayout *vlayout = new QVBoxLayout(this);
//    hlayout->addWidget(searchNameLab);
//    hlayout->addWidget(searchNameEdit);
//    hlayout->addWidget(confirmBtn);
//    vlayout->addLayout(hlayout);
//    vlayout->addWidget(table);

    connect(this->addWid,&AddPlaceWid::dataSaved,this,&PlaceMangerWindow::updataSlots);
    connect(this->editWid,&EditPlaceWid::dataSaved,this,&PlaceMangerWindow::updataSlots);
    connect(this->seaWid,&SearchPlaceWid::seaRes,this,&PlaceMangerWindow::seaRes);

    //tableDataInit();    //map数据初始化

    this->tableUpdate();
}



PlaceMangerWindow::~PlaceMangerWindow()
{

}

void PlaceMangerWindow::tableUpdate()
{
    PlaceManger PM;
    Place *places;
    int sum = 0;
    places = PM.List(sum);
    int count = 0;
    for(int i = 0;i<sum;i++)
    {
        if(strcmp("",places[i].getName())==0)
            continue;
        else
        {
            //qDebug() <<places[i].getX()<<" "<<places[i].getY()<<" "<<places[i].getLeft();
            //qDebug()<<places[i].getName();
            QTableWidgetItem *itemId = new QTableWidgetItem(QString::number(places[i].getId()));
            this->table->setItem(count,0,itemId);
            QTableWidgetItem *itemName = new QTableWidgetItem(places[i].getName());
            this->table->setItem(count,1,itemName);
            QTableWidgetItem *itemDesc = new QTableWidgetItem(places[i].getDesc());
            this->table->setItem(count,2,itemDesc);
            count ++;
        }
    }
}

void PlaceMangerWindow::showInfo(QModelIndex index)
{
    int row = index.row();

    PlaceManger PM;
    if(row>=PM.realCount())
        return;
    if(this->table->item(row,1)==NULL)
        return;
    QString name = this->table->item(row,1)->text();
    QString desc = this->table->item(row,2)->text();
    PlaceInfoWid *info = new PlaceInfoWid();
    info->setName((char *)name.toStdString().c_str());
    info->setDesc((char *)desc.toStdString().c_str());
    info->show();
}

void PlaceMangerWindow::updataSlots()
{
    PlaceManger PM;
    this->mainNameBox->clear();
    this->tableUpdate();
    Place *places;
    int sum = 0;
    places = PM.List(sum);
    for(int i = 0;i<sum;i++)
    {
        if(strcmp(places[i].getName(),"")!=0)
        {
            this->mainNameBox->addItem(places[i].getName());
        }
    }
}

void PlaceMangerWindow::delPlace()
{
    PlaceManger PM;
    int row = this->table->currentRow();
    if(row<0)
    {
        QMessageBox::information(this,"warning","您还没有选择一个景点",QMessageBox::Yes);
        return ;
    }
    if(row>=PM.realCount())
        return ;
    QString name = this->table->item(row,1)->text();

    //删除确认提示框
    int res = QMessageBox::information(this,"确认框","您确定要删除"+name+"吗？",QMessageBox::Yes,QMessageBox::No);
    if(res==QMessageBox::Yes)
    {
        //qDebug()<<"------+"<<name;
        PM.Del((char *)name.toStdString().c_str());
        this->table->removeRow(row);
        this->updataSlots();
    }
    else
    {
        QMessageBox::information(this,"",name+"未删除",QMessageBox::Yes);
    }

}

void PlaceMangerWindow::seaRes(char *name)
{
    this->table->clear();
    QStringList header;
    header<<"编号："<<"地点名称"<<"信息介绍";
    this->table->setHorizontalHeaderLabels(header);
    PlaceManger PM;
    int sum = 0;
    Place *places = PM.List(sum);

    std::string name1 = std::string(name);
    int pos = 0;
    for(int i = 0;i<sum;i++)
    {
        if(strcmp(places[i].getName(),"")==0)
            continue;
        std::string name2 = places[i].getName();
        if(name2.find(name1)!=-1)
        {
            QTableWidgetItem *itemId,*itemName,*itemDesc;
            itemId = new QTableWidgetItem(QString::number(places[i].getId()));
            itemName = new QTableWidgetItem(places[i].getName());
            itemDesc = new QTableWidgetItem(places[i].getDesc());
            this->table->setItem(pos,0,itemId);
            this->table->setItem(pos,1,itemName);
            this->table->setItem(pos++,2,itemDesc);
        }
    }
    if(pos==0)
    {
        QTableWidgetItem *item = new QTableWidgetItem("没有你要查找的地点信息！");
        this->table->setItem(0,0,item);
    }
//    if(row==-1)
//    {
//        QMessageBox::information(this,"waring","没有您要查询的地点信息!",QMessageBox::Yes);
//        return ;
//    }
//    int pos = 0;
//    for(int i = 0;;i++)
//    {
//        if(this->table->item(i,0)->text().toInt()==row+1)
//            break;
//        pos++;
//    }
//    this->table->selectRow(200);
//    this->table->selectRow(pos);
}

void PlaceMangerWindow::dataInit()
{
    this->tableDataInit();
    this->updataSlots();
}

void PlaceMangerWindow::barInit()
{
    QAction *addAction = new QAction(QIcon(":/new/prefix1/Images/add.png"),"新增",this);
    QAction *delAction = new QAction(QIcon(":/new/prefix1/Images/delete.png"),"删除",this);
    QAction *editAction = new QAction(QIcon(":/new/prefix1/Images/edit.png"),"修改",this);
//    QAction *seaAction = new QAction("查找",this);
    QAction *DataInitAction = new QAction("数据初始化",this);

    QToolBar *addBar = addToolBar("新增");
    QToolBar *delBar = addToolBar("删除");
    QToolBar *editBar = addToolBar("修改");
//    QToolBar *seaBar = addToolBar("搜索");
    QToolBar *dataIninBar = addToolBar("数据初始化");

    addBar->addAction(addAction);
    delBar->addAction(delAction);
    editBar->addAction(editAction);
//    seaBar->addAction(seaAction);
    dataIninBar->addAction(DataInitAction);
    connect(addAction,&QAction::triggered,[=](){
        qDebug() <<"----";
        this->addWid->show();
    });
    connect(delAction,&QAction::triggered,this,&PlaceMangerWindow::delPlace);
    connect(editAction,&QAction::triggered,this,[=](){
        PlaceManger PM;
        int row = this->table->currentRow();
        if(row<0)
        {
            QMessageBox::information(this,"warning","您还没有选择一个景点",QMessageBox::Yes);
            return ;
        }
        if(row>=PM.realCount())
            return ;
        this->editWid->show();
        char name[50];
        strcpy(name,this->table->item(row,1)->text().toStdString().c_str());
        this->editWid->setEditName(name);
    });
//    connect(seaAction,&QAction::triggered,this,[=](){
//        this->seaWid->show();
//    });
    connect(DataInitAction,&QAction::triggered,this,&PlaceMangerWindow::dataInit);
}

void PlaceMangerWindow::tableInit()
{
    this->table = new QTableWidget(this);
    this->table->setRowCount(300);
    this->table->setColumnCount(3);
    this->table->resize(this->width(),this->height());
    this->table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//居中
    this->table->verticalHeader()->setVisible(false);
    this->table->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QStringList header;
    header<<"编号："<<"地点名称"<<"信息介绍";
    this->table->setHorizontalHeaderLabels(header);
    this->table->setSelectionBehavior(QAbstractItemView::SelectRows);
    this->table->move(0,100);
    connect(this->table,&QTableWidget::doubleClicked,this,&PlaceMangerWindow::showInfo);
}

void PlaceMangerWindow::tableDataInit()
{
    remove("D://Place.dat");
    node *placeDatas;
    MyTool tool;
    placeDatas = tool.getAllData();
    PlaceManger PM;
    for(int i = 0;i<234;i++)
    {
        Place place(PM.count() + 1,placeDatas[i].name,placeDatas[i].desc);
        place.setX(placeDatas[i].x);
        place.setY(placeDatas[i].y);
        place.setDown(placeDatas[i].down);
        place.setLeft(placeDatas[i].left);
        place.setRight(placeDatas[i].right);
        place.setUp(placeDatas[i].up);
        place.setBicycleNum(30);
        PM.Add(place);
    }
}

void PlaceMangerWindow::SetMainBox(QComboBox *mainNameBox)
{
    this->mainNameBox = mainNameBox;
}
